<?php

/*
	Class: UsersIndex

	About: Author
		J William McCarthy

	About: License
		<http://communit.as/docs/license>

	About: See Also
		<App_Db_Table_Abstract>
*/
class UsersIndex extends App_Db_Table_Abstract {

    protected $_name = 'default_vw_users_index';
    protected $_primary = 'username';

	/* Group: Instance Methods */

    /*
		Function: fetchByUsername
			
	*/
    public function fetchByUsername($username) {
		$where = $this->getAdapter()->quoteInto('username = ?', $username);
        return $this->fetchRow($where);
    }   

    /*
		Function: addMediaForUserArray
	*/
    public function addMediaForUserArray(array $users) {
    	$ret_users = array();
    	foreach ($users as $user) {
			$user['media'] = $this->getProfileMediaArray($user['username']);
    		$ret_users[] = $user;
    	}
    	return $ret_users;
    }

    /*
		Function: userExists
	*/
    public function userExists($username){
    	$where = $this->getAdapter()->quoteInto('username = ?', $username);
    	$user = $this->fetchRow($where);
    	if (!is_null($user)) {
    		return true;
    	} else {
    		return false;
    	}    
    }

    /*
		Function: getProfileMediaArray
	*/
    public function getProfileMediaArray($username) {
    	$umi_table = new UsersMediaIndex();
    	$a_media = array();
    	$media_table = new Media();
    	$where = $this->getAdapter()->quoteInto('username = ? and is_default = 1', $username);
    	$um = $umi_table->fetchRow($where);
    	if (!is_null($um)) {
    		$media = $media_table->fetchRow('id = ' . $um->id);
	    	if (!is_null($media)) {
				$a_media['thumb_image_url'] = $media_table->getThumbImageUrlById($media->id);
				$a_media['full_image_url'] = $media_table->getFullImageUrlById($media->id);
			}	
		}
		if (count($a_media) == 0) {
			$theme_url = Zend_Registry::get('theme_url');
			$a_media['thumb_image_url'] = App_Registry::get('default_profile_icon_url', $theme_url . '/images/defaulticon.png');
			$a_media['full_image_url'] = App_Registry::get('default_profile_fullimage_url', $theme_url . '/images/defaulticon.png');
		}
		return $a_media;
    }
}
